package com.xfg.domain.strategy.model.entity;


import com.xfg.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Fuzhengwei bugstack.cn @小傅哥
 * @description 策略实体
 * @create 2023-12-31 15:24
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StrategyEntity {

    /**
     * 抽奖策略ID
     */
    private Long strategyId;

    /**
     * 抽奖策略描述
     */
    private String strategyDesc;

    /**
     * 抽奖规则模型
     */
    private String ruleModels;

    /**
     * 将规则模型字符串按分隔符拆分为数组
     *
     * @return 规则模型数组，如果规则模型为空则返回null
     */
    public String[] ruleModels() {
        if (StringUtils.isBlank(ruleModels)) return null;
        return ruleModels.split(Constants.SPLIT);
    }

    /**
     * 获取权重规则配置
     * 遍历规则模型数组，查找是否存在名为"rule_weight"的规则
     *
     * @return 权重规则名称，如果不存在则返回null
     */
    public String getRuleWeight() {
        String[] ruleModels = this.ruleModels();
        if (null == ruleModels) { // 必须做判空处理
            return null;
        }
        for (String ruleModel : ruleModels) {
            if ("rule_weight".equals(ruleModel)) return ruleModel;
        }
        return null;
    }




}

